package cn.myjerry.collection;

/**
 * 自定义实现Map的功能
 * 暂不完美
 * Map:存放键值对，根据键对象找对应的值对象，键不能重复
 * @author Jerry
 *
 */

public class SxtMap001 {
	SxtEntry[] arr = new SxtEntry[3];
	private int size;
	
	public int size() {
		return size;
	}
	
	public void put(Object key, Object value) {
		if(size >= arr.length-1) {
			SxtEntry[] newArr = new SxtEntry[size*2+1];
			System.arraycopy(arr, 0, newArr, 0, arr.length);
			arr = newArr;
		}
		SxtEntry e = new SxtEntry(key, value);
		for(int i=0;i<size;i++) {
			if(arr[i].getKey().equals(key)) {
				arr[i].setValue(value);
				return;
			}
		}
		arr[size++] = e;
	}
	
	public Object get(Object key) {
		for(int i=0;i<size;i++) {
			if(arr[i].getKey().equals(key)) {
				return arr[i].getValue();
			}
		}
		return null;
	}
	
	public boolean containsKey(Object key) {
		for(int i=0;i<size;i++) {
			if(arr[i].getKey().equals(key)) {
				return true;
			}
		}
		return false;
	}
	
	public boolean containsValue(Object value) {
		for(int i=0;i<size;i++) {
			if(arr[i].getValue().equals(value)) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		SxtMap001 map = new SxtMap001();
		map.put("Jerry", "123456");
		map.put("Tom", "123456");
		map.put("Davy", "123456");
		map.put("Bobi", "123456");
		map.put("Kim", "123456");
		
		System.out.println(map.size());
	}
}

class SxtEntry {
	private Object key;
	private Object value;
	public SxtEntry() {}
	public SxtEntry(Object key, Object value) {
		super();
		this.key = key;
		this.value = value;
	}
	/**
	 * @return the key
	 */
	public Object getKey() {
		return key;
	}
	/**
	 * @param key the key to set
	 */
	public void setKey(Object key) {
		this.key = key;
	}
	/**
	 * @return the value
	 */
	public Object getValue() {
		return value;
	}
	/**
	 * @param value the value to set
	 */
	public void setValue(Object value) {
		this.value = value;
	}
}